package org.misalen.web.server.controllers.owner;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.misalen.annotations.ControllerApi;
import org.misalen.annotations.InterfaceApi;
import org.misalen.utils.TextUtil;
import org.misalen.web.database.model.mysql.pro.ProAdvertOrder;
import org.misalen.web.database.model.mysql.pro.ProAdvertOrderPayInfo;
import org.misalen.web.database.util.PaginationFrom;
import org.misalen.web.server.config.ErrorCode;
import org.misalen.web.server.config.ServerResult;
import org.misalen.web.server.config.ServerUrl;
import org.misalen.web.server.config.Uncertain;
import org.misalen.web.server.controllers.BaseController;
import org.misalen.web.server.exception.CustomerException;
import org.misalen.web.server.forms.user.AdverOrderAddForm;
import org.misalen.web.server.forms.user.AdvertOrderCancelForm;
import org.misalen.web.server.forms.user.AdvertOrderPrepaidForm;
import org.misalen.web.server.forms.user.AdvertOrderRecordForm;
import org.misalen.web.server.result.Empty;
import org.misalen.web.server.util.FormUtil;
import org.misalen.web.service.pay.alipay.AliPayParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ControllerApi(value = "广告订单", description = "", index = 800)
@RequestMapping(value = ServerUrl.AdvertOrderContrllerUrl)
public class OwnerOrderController extends BaseController {

	@RequestMapping(value = "/add", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "发布广告", resultClass = { Empty.class })
	public @ResponseBody ServerResult add(@RequestBody AdverOrderAddForm form) throws CustomerException {
		FormUtil.validate(form);
		ProAdvertOrder advertOrder = new ProAdvertOrder();
		advertOrder.setAddTime(new Date());
		advertOrder.setUserId(form.getUserId());
		advertOrder.setOrderName(form.getOrderName());
		advertOrder.setOrderNum(form.getOrderNum());
		advertOrder.setStartDate(form.getStartDate());
		advertOrder.setStopDate(form.getStopDate());
		advertOrder.setOrderAmount(form.getOrderAmount());
		advertOrder.setState("noPay");
		proAdvertOrderRepository.save(advertOrder);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderId", advertOrder.getPrimaryKey());
		return ServerResult.ok(map);
	}

	@RequestMapping(value = "/cancel", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "取消广告", resultClass = { Empty.class })
	public @ResponseBody ServerResult cancel(@RequestBody AdvertOrderCancelForm form) throws CustomerException {
		FormUtil.validate(form);
		proAdvertOrderRepository.delete(ProAdvertOrder.class, form.getOrderId());
		proAdvertOrderPayInfoRepository.deleteByOrderId(form.getOrderId());
		return ServerResult.ok(null);
	}


	@RequestMapping(value = "/prepaid", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "预支付", resultClass = { Empty.class })
	public @ResponseBody ServerResult prepaid(HttpServletRequest request, @RequestBody AdvertOrderPrepaidForm form)
			throws Exception {
		FormUtil.validate(form);
		String baseUrl = getBaseUrl(request);
		ProAdvertOrder advertOrder = proAdvertOrderRepository.get(ProAdvertOrder.class, form.getOrderId());
		if (advertOrder.getState().equals("noPay")) {
			ProAdvertOrderPayInfo proAdvertOrderPayInfo = new ProAdvertOrderPayInfo();
			proAdvertOrderPayInfo.setOrderId(form.getOrderId());
			proAdvertOrderPayInfo.setAddTime(new Date());
			proAdvertOrderPayInfo.setPayType(form.getPayType());
			proAdvertOrderPayInfoRepository.save(proAdvertOrderPayInfo);
			if (form.getPayType().equals("alipay")) {
				AliPayParam myParam = new AliPayParam();
				myParam.setBody(Uncertain.advertOrderBody);
				myParam.setSubject(Uncertain.advertisingSubject);
				myParam.setOutTradeNo(proAdvertOrderPayInfo.getOrderId());
				myParam.setTotalAmount(TextUtil.changeF2Y(advertOrder.getOrderAmount().toString()));
				String param = aliPay.getAlipayInfo(myParam, Uncertain.orderAliPayNotifyUrl);
				return ServerResult.ok(param);
			} else {
				String myParam = wechatPay.orderedParam(baseUrl + Uncertain.advertisingBody,
						proAdvertOrderPayInfo.getOrderId(), advertOrder.getOrderAmount().toString(),
						proAdvertOrderPayInfo.getOrderId(), getIpAddress(request), baseUrl + Uncertain.orderWePayNotifyUrl);
				Map<String, Object> map = wechatPay.unifiedorder(myParam);
				return ServerResult.ok(wechatPay.clientParam(map));
			}
		} else {
			return ServerResult.error(ErrorCode.SYS_ERROR_CODE_00001);
		}
	}

	@RequestMapping(value = "/record", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "我的广告订单", resultClass = { PaginationFrom.class, ProAdvertOrder.class })
	public PaginationFrom<ProAdvertOrder> record(HttpServletRequest request,
			@RequestBody AdvertOrderRecordForm form) throws CustomerException {
		FormUtil.validate(form);
		PaginationFrom<ProAdvertOrder> pagination = formToPage(form, ProAdvertOrder.class);
		pagination = proAdvertOrderRepository.findByUser(form.getUserId(), pagination);
		
		return pagination;
	}
	
}
